本文共 1115 字,大约阅读时间需要 3 分钟。
如何安装svelte
Promises are an awesome tool we have at our disposal to work with asynchronous events in JavaScript.
Promise是一个很棒的工具,可用于处理JavaScript中的异步事件。
The relatively recent introduction of the await
syntax in ES2017 made using promises even simpler.
ES2017中相对较新的await
语法介绍使使用promise更加简单。
Svelte provides us the {#await}
syntax in templates to directly work with promises at the template level.
Svelte在模板中为我们提供了{#await}
语法,可直接在模板级别使用promise。
We can wait for promises to resolve, and define a different UI for the various states of a promise: unresolved, resolved and rejected.
我们可以等待承诺解决,并为承诺的各种状态定义不同的UI:未解决,已解决和已拒绝。
Here’s how it works. We define a promise, and using the {#await}
block we wait for it to resolve.
运作方式如下。 我们定义一个承诺,并使用{#await}
块等待它解决。
Once the promise resolves, the result is passed to the {:then}
block:
承诺解决后,结果将传递到{:then}
块:
{#await fetchImage}...waiting
{:then data} {/await}
You can detect a promise rejection by adding a {:catch}
block:
您可以通过添加{:catch}
块来检测承诺被拒绝:
{#await fetchImage}...waiting
{:then data} {:catch error}An error occurred!
{/await}
Run the example:
运行示例: :
翻译自:
如何安装svelte
转载地址:http://rxqgb.baihongyu.com/